package com.example.bxy.testguangxi;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private EditText edit_ip;
    private EditText edit_port;
    private EditText edit_mobile;
    private EditText edt_policestation_code;
    private EditText edt_police_id;
    private EditText edt_o_name;
    private EditText edt_o_identify_num;
    private TextView txt_return_result;
    private Button btn_start;
    private Spinner sp_items;
    private Spinner sp_way;
    private Spinner sp_code;
    private int operate_code;
    private String collect_way;
    private int config_items;
    private String upload_ip;
    private String upload_port;
    /**
     * 被采手机号
     */
    private String mobile = "1900000000";
    /**
     * 警局编号
     */
    private String policestation_code = "abc89757";
    /**
     * 警员编号
     */
    private String police_id = "abc89757123456789";
    /**
     * 被采对象姓名
     */
    private String o_name = "我是测试张三";
    /**
     * 被采对象身份证号
     */
    private String o_identify_num = "41270219910213155564646";
    private String jwt_id = "jwt89757";
    private String response_broadcast = "com.kingnet.eagle.surface.response_broadcast";
    private MsgReceiver msgReceiver;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initListener();
        registerMagReceiver();
    }

    private void registerMagReceiver() {
        //动态注册广播接收器
        msgReceiver = new MsgReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(response_broadcast);
        registerReceiver(msgReceiver, intentFilter);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        //注销广播
        unregisterReceiver(msgReceiver);
        super.onDestroy();
    }

    private void initListener() {
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkEdit()) {
                    txt_return_result.setText("");
                    Intent intent = new Intent();
                    ComponentName component = new ComponentName("com.kingnet.bluetooth", "com.kingnet.eagle.surface.StartAppActivity");
                    intent.setComponent(component);
                    intent.setAction("android.intent.action.MAIN");
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("operate_code", operate_code);
                    intent.putExtra("collect_way", collect_way);
                    intent.putExtra("config_items", config_items);
                    intent.putExtra("upload_ip", upload_ip);
                    intent.putExtra("upload_port", upload_port);
                    intent.putExtra("response_broadcast", response_broadcast);
                    intent.putExtra("mobile", mobile);
                    intent.putExtra("policestation_code", policestation_code);
                    intent.putExtra("police_id", police_id);
                    intent.putExtra("o_name", o_name);
                    intent.putExtra("o_identify_num", o_identify_num);
                    intent.putExtra("jwt_id", jwt_id);
                    MainActivity.this.startActivity(intent);
                }
            }
        });
        sp_code.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.i("sp_code _ onItemSelected _ position=" + position);
                operate_code = position;
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
        sp_items.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.i("sp_items _ onItemSelected _ position=" + position);
                if (position == 0) {
                    config_items = 1;
                } else if (position == 1) {
                    config_items = 2;
                } else if (position == 2) {
                    config_items = 3;
                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
        sp_way.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {//选择item的选择点击监听事件

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                LogUtils.i("sp_way _ onItemSelected _ position=" + position);
                if (position == 0) {
                    collect_way = "kolebt";
                } else if (position == 1) {
                    collect_way = "kolewifi";
                } else if (position == 2) {
                    collect_way = "kolebs";
                } else if (position == 3) {
                    collect_way = "kolewireless";
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });
    }

    private boolean checkEdit() {
        upload_ip = edit_ip.getText().toString().trim();
        upload_port = edit_port.getText().toString().trim();
        policestation_code = edt_policestation_code.getText().toString().trim();
        police_id = edt_police_id.getText().toString().trim();
        mobile = edit_mobile.getText().toString().trim();
        o_name = edt_o_name.getText().toString().trim();
        o_identify_num = edt_o_identify_num.getText().toString().trim();
        if (isEmpty(upload_ip)) {
            ToastUtil.showToast(MainActivity.this, "上传地址不能为空");
            return false;
        }
        if (isEmpty(upload_port)) {
            ToastUtil.showToast(MainActivity.this, "上传端口号不能为空");
            return false;
        }
        if (isEmpty(policestation_code)) {
            ToastUtil.showToast(MainActivity.this, "警局编号不能为空");
            return false;
        }
        if (isEmpty(police_id)) {
            ToastUtil.showToast(MainActivity.this, "警员编号不能为空");
            return false;
        }
        if (isEmpty(mobile)) {
            ToastUtil.showToast(MainActivity.this, "被采对象手机号不能为空");
            return false;
        }
        if (isEmpty(o_name)) {
            ToastUtil.showToast(MainActivity.this, "被采对象姓名不能为空");
            return false;
        }
        if (isEmpty(o_identify_num)) {
            ToastUtil.showToast(MainActivity.this, "被采对象身份证号不能为空");
            return false;
        }
        return true;
    }

    public boolean isEmpty(String str) {
        if (str == null) {
            return true;
        } else if (str.equals("") || str.equals("null")) {
            return true;
        } else {
            return false;
        }
    }

    private void init() {
        edit_ip = (EditText) findViewById(R.id.edit_ip);
        edit_port = (EditText) findViewById(R.id.edit_port);
        edit_mobile = (EditText) findViewById(R.id.edit_mobile);
        edt_policestation_code = (EditText) findViewById(R.id.edt_policestation_code);
        edt_police_id = (EditText) findViewById(R.id.edt_police_id);
        edt_o_identify_num = (EditText) findViewById(R.id.edt_o_identify_num);
        edt_o_name = (EditText) findViewById(R.id.edt_o_name);
        txt_return_result = (TextView) findViewById(R.id.txt_return_result);
        btn_start = (Button) findViewById(R.id.btn_start);
        sp_items = (Spinner) findViewById(R.id.sp_items);
        sp_way = (Spinner) findViewById(R.id.sp_way);
        sp_code = (Spinner) findViewById(R.id.sp_code);
    }

    /**
     * 广播接收器
     *
     * @author len
     */
    public class MsgReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //拿到进度，更新UI
            int response_result = intent.getIntExtra("response_result", 0);//采集结果  ( 0上传成功/1采集失败/ 2上传失败)
            String response_error = intent.getStringExtra("response_error");//失败原因
            String case_id = intent.getStringExtra("case_id");//案件编号
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("response_result:" + response_result + "\n");
            stringBuffer.append("response_error:" + response_error + "\n");
            stringBuffer.append("case_id:" + case_id);
            LogUtils.i("MsgReceiver++++  " + stringBuffer.toString());
            txt_return_result.setText("" + stringBuffer.toString());
        }

    }
}
